package com.example.dto;

import com.example.model.RiskIndicator;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * 风控指标数据传输对象
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class RiskIndicatorDTO {

    private String id;
    private String userId;
    private String type;
    private String name;
    private Double value;
    private String riskLevel;
    private LocalDateTime calculatedAt;
    private LocalDateTime validUntil;
    private String source;
    private String description;
    private Map<String, Object> attributes;

    /**
     * 将DTO转换为实体对象
     */
    public RiskIndicator toEntity() {
        return RiskIndicator.builder()
                .id(this.id)
                .userId(this.userId)
                .type(this.type)
                .name(this.name)
                .value(this.value)
                .riskLevel(this.riskLevel != null ? RiskIndicator.RiskLevel.valueOf(this.riskLevel) : null)
                .calculatedAt(this.calculatedAt)
                .validUntil(this.validUntil)
                .source(this.source)
                .description(this.description)
                .attributes(this.attributes)
                .build();
    }

    /**
     * 将实体对象转换为DTO
     */
    public static RiskIndicatorDTO fromEntity(RiskIndicator entity) {
        return RiskIndicatorDTO.builder()
                .id(entity.getId())
                .userId(entity.getUserId())
                .type(entity.getType())
                .name(entity.getName())
                .value(entity.getValue())
                .riskLevel(entity.getRiskLevel() != null ? entity.getRiskLevel().name() : null)
                .calculatedAt(entity.getCalculatedAt())
                .validUntil(entity.getValidUntil())
                .source(entity.getSource())
                .description(entity.getDescription())
                .attributes(entity.getAttributes())
                .build();
    }
}